package com.demo.jsf;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import com.demo.javabean.DBAccess;

public class WorklogAction extends BaseAction {

	private String id;
	private String username;
	private String year;
	private String month;
	private String day;
	private String title;
	private String description;
	private String logtime;

	public String list() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得分页参数
		String strPageSize = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageSize");
		if (strPageSize != null) {
			pageSize = Integer.parseInt(strPageSize);
		}
		String strPageNo = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageNo");
		if (strPageNo != null) {
			pageNo = Integer.parseInt(strPageNo);
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 查询总的记录数，计算跳页参数
			int start = pageSize * (pageNo - 1);
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from worklog where username='"
					+ loginAction.getUsername() + "'";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from worklog where username='" + loginAction.getUsername()
			+ "' order by year desc, month desc, day desc limit "
			+ start + "," + pageSize;
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			while (db.next()) {
				// 查询每行数据的各个字段数据
				Hashtable<String, String> hash = new Hashtable<String, String>();
				hash.put("id", db.getValue("id"));
				hash.put("username", db.getValue("username"));
				hash.put("year", db.getValue("year"));
				hash.put("month", db.getValue("month"));
				hash.put("day", db.getValue("day"));
				hash.put("title", db.getValue("title"));
				hash.put("description", db.getValue("description"));
				hash.put("logtime", db.getValue("logtime"));

				// 保存当前行
				list.add(hash);
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return "workloglist";
	}

	public String add() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		return "worklogadd";
	}

	public String insert() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			logtime = format.format(new Date());

			// 组合新增SQL
			String sql = "insert into worklog (username, year, month, day, title, description, logtime) ";
			sql += " values('" + loginAction.getUsername() + "'," + year + "," + month + ","
					+ day + ",'" + title + "','" + description + "','"
					+ logtime + "')";

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行插入
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String edit() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "workloglist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 根据id编号查询当前行记录
			String sql = "select * from worklog where id=" + id;
			db.query(sql);
			if (db.next()) {
				// 取得各个字段的值并保存
				username = db.getValue("username");
				year = db.getValue("year");
				month = db.getValue("month");
				day = db.getValue("day");
				title = db.getValue("title");
				description = db.getValue("description");
				logtime = db.getValue("logtime");
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		return "worklogedit";
	}

	public String update() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 组合更新SQL
			String sql = "update worklog set year=" + year + ", month=" + month
			+ ", day=" + day + ", title='" + title + "', description='"
			+ description + "' where id=" + id;

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行更新
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String delete() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "workloglist";
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 根据id组成删除SQL，执行删除
			String sql = "delete from worklog where id=" + id;
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLogtime() {
		return logtime;
	}

	public void setLogtime(String logtime) {
		this.logtime = logtime;
	}
}